clear all
set more off

global mun "H:/Stata/Data/municipality"
global admin "H:/Stata/Data/"
global temp "H:/Stata/Data/temp"

** defining treated

use "$temp/admin_mun_v2", clear

program drop treatdefine_total_2C
program define treatdefine_total_2C

gen treat_total_2C = 1 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region == 1 
replace treat_total_2C = 1 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region == 1 
replace treat_total_2C = 1 if yob == 1945  & all_urban_40  == 1 & region == 1 
replace treat_total_2C = 1 if yob == 1946 & mob == 1 & all_urban_40  == 1 & region == 1 

replace treat_total_2C = 0 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region == 0
replace treat_total_2C = 0 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region == 0
replace treat_total_2C = 0 if yob == 1945 & all_urban_40  == 1 & region == 0
replace treat_total_2C = 0 if yob == 1946 & mob == 1& all_urban_40  == 1 & region == 0

end


gen allurban40_norotterdam = all_urban_40
replace allurban40_norotterdam = . if vrlgbageboortegemeente == 599

program drop treatdefine_total_2C_norotterdam
program define treatdefine_total_2C_norotterdam

gen treat_total_2C_norotterdam = 1 if yob == 1944 & mob == 11 & allurban40_norotterdam  == 1 & region == 1 
replace treat_total_2C_norotterdam = 1 if yob == 1944 & mob == 12 & allurban40_norotterdam  == 1 & region == 1 
replace treat_total_2C_norotterdam = 1 if yob == 1945  & allurban40_norotterdam  == 1 & region == 1 
replace treat_total_2C_norotterdam = 1 if yob == 1946 & mob == 1 & allurban40_norotterdam  == 1 & region == 1 

replace treat_total_2C_norotterdam = 0 if yob == 1944 & mob == 11 & allurban40_norotterdam  == 1 & region == 0
replace treat_total_2C_norotterdam = 0 if yob == 1944 & mob == 12 & allurban40_norotterdam  == 1 & region == 0
replace treat_total_2C_norotterdam = 0 if yob == 1945 & allurban40_norotterdam  == 1 & region == 0
replace treat_total_2C_norotterdam = 0 if yob == 1946 & mob == 1& allurban40_norotterdam  == 1 & region == 0

end


program drop treat_sociobefore
program define treat_sociobefore 

gen treat_sociobefore = 1 if yob == 1941 & mob == 9 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1941 & mob == 10 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1941 & mob == 11 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1941 & mob == 12 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 1 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 2 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 3 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 4 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 5 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 6 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 7 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 8 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 9 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 10 & all_urban_40  == 1 & region == 1
replace treat_sociobefore = 1 if yob == 1942 & mob == 11 & all_urban_40  == 1 & region == 1


replace treat_sociobefore = 0 if yob == 1941 & mob == 9 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1941 & mob == 10 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1941 & mob == 11 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1941 & mob == 12 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 1 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 2 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 3 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 4 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 5 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 6 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 7 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 8 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 9 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 10 & all_urban_40  == 1 & region == 0
replace treat_sociobefore = 0 if yob == 1942 & mob == 11 & all_urban_40  == 1 & region == 0

end

program drop treat_sociobefore5yo // those who were 5 years old at the start of the famine
program define treat_sociobefore5yo 

gen treat_sociobefore5yo = 1 if yob == 1938 & mob == 9 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1938 & mob == 10 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1938 & mob == 11 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1938 & mob == 12 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 1 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 2 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 3 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 4 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 5 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 6 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 7 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 8 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 9 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 10 & all_urban_40  == 1 & region == 1
replace treat_sociobefore5yo = 1 if yob == 1939 & mob == 11 & all_urban_40  == 1 & region == 1


replace treat_sociobefore5yo = 0 if yob == 1938 & mob == 9 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1938 & mob == 10 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1938 & mob == 11 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1938 & mob == 12 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 1 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 2 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 3 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 4 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 5 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 6 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 7 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 8 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 9 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 10 & all_urban_40  == 1 & region == 0
replace treat_sociobefore5yo = 0 if yob == 1939 & mob == 11 & all_urban_40  == 1 & region == 0

end

program drop treat_sociobefore10yo // those who were 10 years old at the start of the famine
program define treat_sociobefore10yo 

gen treat_sociobefore10yo = 1 if yob == 1933 & mob == 9 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1933 & mob == 10 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1933 & mob == 11 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1933 & mob == 12 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 1 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 2 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 3 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 4 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 5 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 6 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 7 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 8 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 9 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 10 & all_urban_40  == 1 & region == 1
replace treat_sociobefore10yo = 1 if yob == 1934 & mob == 11 & all_urban_40  == 1 & region == 1


replace treat_sociobefore10yo = 0 if yob == 1933 & mob == 9 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1933 & mob == 10 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1933 & mob == 11 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1933 & mob == 12 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 1 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 2 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 3 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 4 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 5 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 6 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 7 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 8 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 9 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 10 & all_urban_40  == 1 & region == 0
replace treat_sociobefore10yo = 0 if yob == 1934 & mob == 11 & all_urban_40  == 1 & region == 0

end

program drop treat_socioafter
program define treat_socioafter 

gen treat_socioafter = 1 if yob == 1949 & mob == 11 & all_urban_40  == 1 & region == 1
replace treat_socioafter = 1 if yob == 1949 & mob == 12 & all_urban_40  == 1 & region == 1
replace treat_socioafter = 1 if yob == 1950 & all_urban_40  == 1 & region == 1
replace treat_socioafter = 1 if yob == 1951 & mob == 1 & all_urban_40  == 1 & region == 1

replace treat_socioafter = 0 if yob == 1949 & mob == 11 & all_urban_40  == 1 & region == 0
replace treat_socioafter = 0 if yob == 1949 & mob == 12 & all_urban_40  == 1 & region == 0
replace treat_socioafter = 0 if yob == 1950 & all_urban_40  == 1 & region == 0
replace treat_socioafter = 0 if yob == 1951 & mob == 1 & all_urban_40  == 1 & region == 0

end

program drop treat_socioafter2yo
program define treat_socioafter2yo 

gen treat_socioafter2yo = 1 if yob == 1947 & mob == 11 & all_urban_40  == 1 & region == 1
replace treat_socioafter2yo = 1 if yob == 1947 & mob == 12 & all_urban_40  == 1 & region == 1
replace treat_socioafter2yo = 1 if yob == 1948 & all_urban_40  == 1 & region == 1
replace treat_socioafter2yo = 1 if yob == 1949 & mob == 1 & all_urban_40  == 1 & region == 1

replace treat_socioafter2yo = 0 if yob == 1947 & mob == 11 & all_urban_40  == 1 & region == 0
replace treat_socioafter2yo = 0 if yob == 1947 & mob == 12 & all_urban_40  == 1 & region == 0
replace treat_socioafter2yo = 0 if yob == 1948 & all_urban_40  == 1 & region == 0
replace treat_socioafter2yo = 0 if yob == 1949 & mob == 1 & all_urban_40  == 1 & region == 0

end


program drop treat_socioafter10yo
program define treat_socioafter10yo 

gen treat_socioafter10yo = 1 if yob == 1954 & mob == 11 & all_urban_40  == 1 & region == 1
replace treat_socioafter10yo = 1 if yob == 1954 & mob == 12 & all_urban_40  == 1 & region == 1
replace treat_socioafter10yo = 1 if yob == 1955 & all_urban_40  == 1 & region == 1
replace treat_socioafter10yo = 1 if yob == 1956 & mob == 1 & all_urban_40  == 1 & region == 1

replace treat_socioafter10yo = 0 if yob == 1954 & mob == 11 & all_urban_40  == 1 & region == 0
replace treat_socioafter10yo = 0 if yob == 1954 & mob == 12 & all_urban_40  == 1 & region == 0
replace treat_socioafter10yo = 0 if yob == 1955 & all_urban_40  == 1 & region == 0
replace treat_socioafter10yo = 0 if yob == 1956 & mob == 1 & all_urban_40  == 1 & region == 0

end


program drop treatdefine_total_2C_male
program define treatdefine_total_2C_male

gen treat_total_2C_male = 1 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region == 1 & female == 0
replace treat_total_2C_male = 1 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region == 1 & female == 0
replace treat_total_2C_male = 1 if yob == 1945  & all_urban_40  == 1 & region == 1 & female == 0
replace treat_total_2C_male = 1 if yob == 1946 & mob == 1 & all_urban_40  == 1 & region == 1 & female == 0

replace treat_total_2C_male = 0 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region == 0 & female == 0
replace treat_total_2C_male = 0 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region == 0 & female == 0
replace treat_total_2C_male = 0 if yob == 1945 & all_urban_40  == 1 & region == 0 & female == 0
replace treat_total_2C_male = 0 if yob == 1946 & mob == 1 & all_urban_40  == 1 & region == 0 & female == 0

end



program drop treatdefine_total_2C_female
program define treatdefine_total_2C_female

gen treat_total_2C_female = 1 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region == 1 & female == 1
replace treat_total_2C_female = 1 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region == 1 & female == 1
replace treat_total_2C_female = 1 if yob == 1945  & all_urban_40  == 1 & region == 1 & female == 1
replace treat_total_2C_female = 1 if yob == 1946 & mob == 1 & all_urban_40  == 1 & region == 1 & female == 1

replace treat_total_2C_female = 0 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region == 0 & female == 1
replace treat_total_2C_female = 0 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region == 0 & female == 1
replace treat_total_2C_female = 0 if yob == 1945 & all_urban_40  == 1 & region == 0 & female == 1
replace treat_total_2C_female = 0 if yob == 1946 & mob == 1 & all_urban_40  == 1 & region == 0 & female == 1

end


program drop treat_conception
program define treat_conception 

gen treat_conception = 1 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region == 1 
replace treat_conception = 1 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region == 1 
replace treat_conception = 1 if yob == 1945 & mob == 1  & all_urban_40  == 1 & region == 1 
replace treat_conception = 1 if yob == 1945 & mob == 2  & all_urban_40  == 1 & region == 1 
replace treat_conception = 1 if yob == 1945 & mob == 3  & all_urban_40  == 1 & region == 1 
replace treat_conception = 1 if yob == 1945 & mob == 4  & all_urban_40  == 1 & region == 1 
replace treat_conception = 1 if yob == 1945 & mob == 5  & all_urban_40  == 1 & region == 1 
replace treat_conception = 1 if yob == 1945 & mob == 6  & all_urban_40  == 1 & region == 1 
replace treat_conception = 1 if yob == 1945 & mob == 7  & all_urban_40  == 1 & region == 1 
replace treat_conception = 1 if yob == 1945 & mob == 8  & all_urban_40  == 1 & region == 1 

replace treat_conception = 0 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region == 0
replace treat_conception = 0 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region == 0
replace treat_conception = 0 if yob == 1945 & mob == 1 &  all_urban_40  == 1 & region == 0
replace treat_conception = 0 if yob == 1945 & mob == 2 &  all_urban_40  == 1 & region == 0
replace treat_conception = 0 if yob == 1945 & mob == 3 &  all_urban_40  == 1 & region == 0
replace treat_conception = 0 if yob == 1945 & mob == 4 &  all_urban_40  == 1 & region == 0
replace treat_conception = 0 if yob == 1945 & mob == 5 &  all_urban_40  == 1 & region == 0
replace treat_conception = 0 if yob == 1945 & mob == 6 &  all_urban_40  == 1 & region == 0
replace treat_conception = 0 if yob == 1945 & mob == 7 &  all_urban_40  == 1 & region == 0
replace treat_conception = 0 if yob == 1945 & mob == 8 &  all_urban_40  == 1 & region == 0

end



treatdefine_total_2C 
treat_conception
treat_sociobefore
treat_sociobefore10yo
treat_sociobefore5yo
treat_socioafter
treat_socioafter2yo
treat_socioafter10yo
treatdefine_total_2C_male
treatdefine_total_2C_female
treatdefine_total_2C_norotterdam


drop  muncode  birthprovince  conti_urban stein_urban  


gen region_residence = 1 if ///
pro2017 == 26 | ///
pro2017 == 27 | ///
pro2017 == 28
replace region_residence = 0 if ///
pro2017 == 20 | ///
pro2017 == 21 | ///
pro2017 == 22 | ///
pro2017 == 23 | ///
pro2017 == 24 | ///
pro2017 == 25 | ///
pro2017 == 29 | ///
pro2017 == 30 | ///
pro2017 == 31

** defining control

gen control_total_2C = 1 if  treat_total_2C == 0 
replace control_total_2C = 0 if treat_total_2C == 1

gen control_conception = 1 if treat_conception == 0 
replace control_conception = 0 if treat_conception == 1

gen control_sociobefore = 1 if treat_sociobefore == 0 
replace control_sociobefore = 0 if treat_sociobefore == 1

gen control_sociobefore5yo = 1 if treat_sociobefore5yo == 0 
replace control_sociobefore5yo = 0 if treat_sociobefore5yo == 1

gen control_sociobefore10yo = 1 if treat_sociobefore10yo == 0 
replace control_sociobefore10yo = 0 if treat_sociobefore10yo == 1 

gen control_socioafter = 1 if treat_socioafter == 0 
replace control_socioafter = 0 if treat_socioafter == 1

gen control_socioafter2yo = 1 if treat_socioafter2yo == 0 
replace control_socioafter2yo = 0 if treat_socioafter2yo == 1

gen control_socioafter10yo = 1 if treat_socioafter10yo == 0 
replace control_socioafter10yo = 0 if treat_socioafter10yo == 1

drop all_urban_3040

gen control_treatmale = 1 if treat_total_2C_male == 0
replace control_treatmale = 0 if treat_total_2C_male == 1

gen control_treatfemale = 1 if treat_total_2C_female == 0 
replace control_treatfemale = 0 if treat_total_2C_female == 1


drop vrlgbageboortegemeente pro2017

gen control_norotterdam = 1 if treat_total_2C_norotterdam == 0 
replace control_norotterdam = 0 if treat_total_2C_norotterdam == 1


drop  allurban40_norotterdam
drop region all_urban_40
egen double group = group(RINPERSOON RINPERSOONS)

** keeping only those who are above 18 (voting age)
drop age 
gen age = year - yob
 
gen adult = 1 if age >=18 & age!=. 

keep if adult == 1 
drop adult
 
bysort formateur year: egen mun_people_count = count(group) // total people (RINPERSOON RINPERSOONS) in municipality



** municipality movers 

gen time = . 
replace time = 1 if year == 1998
replace time = 2 if year == 2002
replace time = 3 if year == 2003
replace time = 4 if year == 2006
replace time = 5 if year == 2010
replace time = 6 if year == 2012
replace time = 7 if year == 2017

xtset group time 

gen lagresidence = l.residence
bysort group: gen step = _n
bysort group: replace lagresidence=  residence if step == 1

****************
* Table 4 
****************

* ALL MOVERS

gen munmover = 1 if residence != lagresidence
replace munmover = 0 if residence == lagresidence

tab munmover
tab munmover region_residence, row


bysort formateur year: egen movers_total = sum (munmover)
gen movers_pop  = movers_total/mun_people_count 
drop movers_total

* TREATED MOVERS

gen treat_munmover = 1 if (residence != lagresidence) & treat_total_2C == 1
replace treat_munmover = 0 if (residence == lagresidence) & treat_total_2C == 1
tab treat_munmover
tab treat_munmover region_residence, row

drop region_residence

bysort formateur year: egen treat_movers_total = sum (treat_munmover)
gen treat_movers_pop  = treat_movers_total/mun_people_count 
drop time lagresidence  step  treat_movers_total 
 
 

*total
bysort formateur year: egen treat_total_2C_agg_treated = sum (treat_total_2C) // number of treated
gen treat_total_2C_pop = treat_total_2C_agg_treated/mun_people_count 
drop   treat_total_2C 



bysort formateur year: egen control_total_2C_agg_treated = sum (control_total_2C) // number of treated
gen control_total_2C_pop = control_total_2C_agg_treated/mun_people_count 
drop control_total_2C_agg_treated control_total_2C


* no rotterdam

bysort formateur year: egen treat_norotterdam_agg_treated = sum (treat_total_2C_norotterdam) // number of treated
gen treat_total_2C_norotterdam_pop = treat_norotterdam_agg_treated/mun_people_count 

bysort formateur year: egen control_norotterdam_agg_treated = sum (control_norotterdam) // number of treated
gen control_norotterdam_pop = control_norotterdam_agg_treated/mun_people_count 
drop treat_norotterdam_agg_treated treat_total_2C_norotterdam control_norotterdam_agg_treated control_norotterdam 

*conception 
 
bysort formateur year: egen treat_conception_agg_treated = sum (treat_conception) // number of treated
gen treat_conception_pop = treat_conception_agg_treated/mun_people_count 

bysort formateur year: egen control_conception_agg_treated = sum (control_conception) // number of treated
gen control_conception_pop = control_conception_agg_treated/mun_people_count 
drop  treat_conception treat_conception_agg_treated  control_conception_agg_treated  control_conception // 


*socio before

bysort formateur year: egen treat_socio_before_agg = sum (treat_sociobefore) // number of treated
gen treat_socio_before_pop = treat_socio_before_agg/mun_people_count 

bysort formateur year: egen control_socio_before_agg = sum (control_sociobefore) // number of treated
gen control_socio_before_pop = control_socio_before_agg/mun_people_count 

drop treat_socio_before_agg treat_sociobefore control_socio_before_agg control_sociobefore

*socio after

bysort formateur year: egen treat_socio_after_agg = sum (treat_socioafter) // number of treated
gen treat_socio_after_pop = treat_socio_after_agg/mun_people_count 

bysort formateur year: egen control_socio_after_agg = sum (control_socioafter) // number of treated
gen control_socio_after_pop = control_socio_after_agg/mun_people_count 

drop treat_socio_after_agg treat_socioafter control_socio_after_agg control_socioafter

*socio before 5yo
bysort formateur year: egen treat_socio_before5yo_agg = sum (treat_sociobefore5yo) // number of treated
gen treat_socio_before5yo_pop = treat_socio_before5yo_agg/mun_people_count 
drop treat_socio_before5yo_agg treat_sociobefore5yo

*socio before 10yo
bysort formateur year: egen treat_socio_before10yo_agg = sum (treat_sociobefore10yo) // number of treated
gen treat_socio_before10yo_pop = treat_socio_before10yo_agg/mun_people_count 
drop treat_socio_before10yo_agg treat_sociobefore10yo

*socio after2yo

bysort formateur year: egen treat_socio_after2yo_agg = sum (treat_socioafter2yo) // number of treated
gen treat_socio_after2yo_pop = treat_socio_after2yo_agg/mun_people_count 

drop treat_socio_after2yo_agg treat_socioafter2yo 


*socio after10yo

bysort formateur year: egen treat_socio_after10yo_agg = sum (treat_socioafter10yo) // number of treated
gen treat_socio_after10yo_pop = treat_socio_after10yo_agg/mun_people_count 

drop treat_socio_after10yo_agg treat_socioafter10yo 



*treated males

bysort formateur year: egen mun_men_count = count(group) if female ==0 
bysort formateur year: egen mun_men_count_FINAL = mean(mun_men_count)  
drop mun_men_count 


bysort formateur year: egen treat_male_agg_treated = sum (treat_total_2C_male) // number of treated
gen treat_male_pop = treat_male_agg_treated/mun_men_count_FINAL 

bysort formateur year: egen control_male_agg_treated = sum (control_treatmale) // number of treated
gen control_male_pop = control_male_agg_treated/mun_men_count_FINAL 

drop treat_male_agg_treated treat_total_2C_male control_male_agg_treated control_treatmale mun_men_count

*treated female
bysort formateur year: egen mun_female_count = count(group) if female == 1 
bysort formateur year: egen mun_female_count_FINAL = mean(mun_female_count)  
drop mun_female_count

bysort formateur year: egen treat_female_agg_treated = sum (treat_total_2C_female) // number of treated
gen treat_female_pop = treat_female_agg_treated/mun_female_count_FINAL 

bysort formateur year: egen control_female_agg_treated = sum (control_treatfemale) // number of treated
gen control_female_pop = control_female_agg_treated/mun_female_count_FINAL 

drop treat_female_agg_treated treat_total_2C_female control_female_agg_treated control_treatfemale mun_female_count


save "$temp/admin_mun2_urban40", replace

duplicates drop formateur year, force

merge 1:1 formateur year using "$mun/municipality_v2" // bringing in mun vars
keep if _merge == 3 // 2715
drop _merge

drop RINPERSOON RINPERSOONS group

order residence formateur year

merge m:1 formateur using "$temp/mun_prov"
drop _merge
save "$temp/admin_mun2_FINAL_urban40", replace

rm "$temp/mun_prov.dta"
rm "$temp/admin_mun2_urban40.dta"




